<template>
  <div class="article-detail">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="decoration-circle"></div>
      <div class="decoration-square"></div>
      <div class="decoration-dots"></div>
    </div>

    <div class="top-gradient-bar"></div>
    
    <div class="article-container">
      <!-- 文章头部信息 -->
      <header class="article-header">
        <div class="category-tag" v-if="article.category">
          {{ article.category }}
        </div>
        
        <h1 class="title">
          {{ article.title }}
          <span v-if="article.isOriginal" class="original-badge">
            {{ currentLanguage === 'zh' ? '原创' : 'Original' }}
          </span>
        </h1>

        <div class="meta-info">
          <div class="meta-item">
            <i class="fas fa-calendar"></i>
            {{ article.publishedDate }}
          </div>
          <div class="meta-item">
            <i class="fas fa-user"></i>
            {{ article.authorName }}
          </div>
          <div class="meta-item">
            <i class="fas fa-eye"></i>
            {{ article.views }}
          </div>
          <div class="language-switch" v-if="article.hasEnglishVersion" @click="switchLanguageArticle">
            <i class="fas fa-language"></i>
            {{ articleLanguage === 'zh' ? 'Switch to English' : '切换到中文' }}
          </div>
        </div>
      </header>

      <!-- 修改内容区域结构 -->
      <div class="content-wrapper">
        <!-- PC端目录 -->
        <nav v-if="isPC" class="toc" :class="{ 'toc-fixed': isScrolled }">
          <div class="toc-header">
            <i class="fas fa-list"></i>
            目录
          </div>
          <div class="toc-content">
            <div v-for="(item, index) in tableOfContents" 
                 :key="index"
                 class="toc-item"
                 :class="{ 'active': currentSection === index }"
                 @click="scrollToSection(item.id)">
              {{ item.title }}
            </div>
          </div>
        </nav>

        <!-- 文章主体内容区域 -->
        <div class="main-content">
          <!-- 装饰元素 -->
          <div class="content-decoration">
            <div class="decoration-line"></div>
            <div class="reading-progress" :style="{ width: readingProgress + '%' }"></div>
          </div>

          <article class="article-content" v-html="article.content"></article>
          
          <!-- 文章底部互动区 -->
          <div class="interaction-area">
            <div class="like-section">
              <button class="like-btn" @click="handleLike">
                <i class="fas fa-heart" :class="{ 'liked': isLiked }"></i>
                <span>{{ likeCount }}</span>
              </button>
            </div>
            
            <div class="share-section">
              <button class="share-btn" @click="shareToWeixin">
                <i class="fab fa-weixin"></i>
              </button>
              <button class="share-btn" @click="shareToWeibo">
                <i class="fab fa-weibo"></i>
              </button>
              <button class="share-btn" @click="shareToTwitter">
                <i class="fab fa-twitter"></i>
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 返回顶部按钮 -->
    <div class="float-actions" v-show="showToTopButton">
      <button class="action-btn top-btn" @click="scrollToTop">
        <i class="fas fa-arrow-up"></i>
      </button>
    </div>
  </div>
</template>

<style scoped>
.article-detail {
  min-height: 100vh;
  background-color: #f8f9fa;
}

.top-gradient-bar {
  height: 4px;
  background: linear-gradient(90deg, #00c6fb 0%, #005bea 100%);
}

.article-container {
  max-width: 90%;
  margin: 0 auto;
  padding: 2rem 3rem;
  background: #fff;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  border-radius: 16px;
  margin-top: 2rem;
  position: relative;
  z-index: 1;
}

.article-header {
  margin-bottom: 3rem;
}

.category-tag {
  display: inline-block;
  padding: 0.4rem 1rem;
  background: #e9ecef;
  border-radius: 20px;
  font-size: 0.9rem;
  color: #495057;
  margin-bottom: 1rem;
}

.title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #212529;
  margin-bottom: 1.5rem;
  line-height: 1.4;
}

.original-badge {
  display: inline-block;
  padding: 0.3rem 0.8rem;
  background: linear-gradient(45deg, #FF416C 0%, #FF4B2B 100%);
  color: white;
  border-radius: 4px;
  font-size: 0.9rem;
  margin-left: 1rem;
  vertical-align: middle;
}

.meta-info {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  color: #6c757d;
  font-size: 0.95rem;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.content-wrapper {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 2rem;
  position: relative;
}

@media (min-width: 1024px) {
  .content-wrapper {
    /* grid-template-columns: 250px minmax(0, 1fr); */
  }
}

.main-content {
  position: relative;
  max-width: 100%;
  margin: 0;
  width: 100%;
}

.toc {
  background: #fff;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  width: 250px;
}

.toc-fixed {
  position: fixed;
  top: 2rem;
  width: 250px;
}

.toc-header {
  font-weight: 600;
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid #e9ecef;
}

.toc-item {
  padding: 0.5rem;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 4px;
}

.toc-item:hover {
  background: #f8f9fa;
  color: #007bff;
}

.toc-item.active {
  background: #e7f5ff;
  color: #007bff;
}

.article-content {
  font-size: 1.1rem;
  line-height: 1.8;
  color: #2c3e50;
  padding: 0 1rem;
  width: 100%;
}

.article-content h1,
.article-content h2,
.article-content h3 {
  margin: 2rem 0 1rem;
  color: #1a202c;
  font-weight: 600;
}

.article-content p {
  margin-bottom: 1.5rem;
  text-align: justify;
  font-size: 1.1rem;
  line-height: 1.8;
}

.article-content img {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  margin: 2rem auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.article-content img:hover {
  transform: scale(1.02);
}

.interaction-area {
  margin-top: 4rem;
  padding: 2rem;
  border-top: 1px solid #e9ecef;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.like-btn {
  background: none;
  border: 2px solid #ff4b2b;
  border-radius: 30px;
  padding: 0.8rem 2rem;
  color: #ff4b2b;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.like-btn:hover {
  background: #ff4b2b;
  color: white;
}

.like-btn i.liked {
  color: #ff4b2b;
  animation: heartBeat 0.3s ease-in-out;
}

.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 4px;
  background: linear-gradient(90deg, #00c6fb, #005bea);
  z-index: 1000;
  transition: width 0.3s ease;
}

.float-actions {
  position: fixed;
  right: 2rem;
  bottom: 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.action-btn {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: none;
  background: white;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

/* 背景装饰 */
.background-decoration {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.decoration-circle {
  position: absolute;
  top: 10%;
  right: 5%;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: linear-gradient(45deg, rgba(0, 198, 251, 0.1), rgba(0, 91, 234, 0.1));
  animation: float 8s infinite ease-in-out;
}

.decoration-square {
  position: absolute;
  bottom: 15%;
  left: 5%;
  width: 200px;
  height: 200px;
  background: linear-gradient(135deg, rgba(255, 65, 108, 0.1), rgba(255, 75, 43, 0.1));
  transform: rotate(45deg);
  animation: float 6s infinite ease-in-out reverse;
}

.decoration-dots {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(circle, #e9ecef 1px, transparent 1px);
  background-size: 30px 30px;
  opacity: 0.3;
}

/* 动画效果 */
@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0);
  }
  50% {
    transform: translateY(-20px) rotate(5deg);
  }
}

@keyframes heartBeat {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.3);
  }
  100% {
    transform: scale(1);
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .article-container {
    max-width: 100%;
    padding: 1rem;
    margin: 0;
    border-radius: 0;
  }

  .article-content {
    padding: 0 0.5rem;
  }
}

/* 中等屏幕尺寸适配 */
@media (min-width: 769px) and (max-width: 1200px) {
  .article-container {
    max-width: 95%;
    padding: 2rem;
  }

  .content-wrapper {
    grid-template-columns: 200px minmax(0, 1fr);
  }
}

/* 大屏幕优化 */
@media (min-width: 1920px) {
  .article-container {
    max-width: 95%;
  }

  .main-content {
    max-width: none;
  }
}

/* 优化文章内容的排版 */
.article-content h1 {
  font-size: 2.2rem;
  margin: 2.5rem 0 1.5rem;
}

.article-content h2 {
  font-size: 1.8rem;
  margin: 2rem 0 1.2rem;
}

.article-content h3 {
  font-size: 1.5rem;
  margin: 1.8rem 0 1rem;
}

.article-content p {
  margin-bottom: 1.5rem;
  text-align: justify;
  font-size: 1.1rem;
  line-height: 1.8;
}

/* 优化代码块显示 */
.article-content pre {
  margin: 1.5rem 0;
  padding: 1.5rem;
  border-radius: 8px;
  background: #f8f9fa;
  overflow-x: auto;
  width: 100%;
}

.article-content code {
  font-family: 'Fira Code', monospace;
  font-size: 0.95rem;
}

/* 优化列表显示 */
.article-content ul,
.article-content ol {
  margin: 1.5rem 0;
  padding-left: 2rem;
}

.article-content li {
  margin-bottom: 0.5rem;
}

/* 优化表格显示 */
.article-content table {
  width: 100%;
  margin: 2rem 0;
  border-collapse: collapse;
}

.article-content th,
.article-content td {
  padding: 1rem;
  border: 1px solid #e9ecef;
}

.article-content th {
  background: #f8f9fa;
  font-weight: 600;
}

/* 优化引用块显示 */
.article-content blockquote {
  margin: 2rem 0;
  padding: 1rem 2rem;
  border-left: 4px solid #007bff;
  background: #f8f9fa;
  font-style: italic;
}
</style>

<script>
import { messages } from '@/languages/articleDetail-language.js';
import axios from 'axios'; // 引入 Axios 用于发送请求
import { mapGetters } from 'vuex'; // 如果使用 Vuex，可以用来访问 getters

export default {
  name: "ArticleDetail",
  props: {
    currentLanguage: {
      type: String,
      default: 'zh',
    },
    isPC: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      messages: messages,
      tableOfContents: [],
      showToTopButton: false, // 控制按钮是否显示
      article: {}, // 初始化文章对象
      articleLanguage: 'zh', // 文章语言，默认为中文
      readingProgress: 0,
      isLiked: false,
      likeCount: 0,
    };
  },
  mounted() {
    this.fetchArticleDetails(); // 获取文章详情
    this.generateTableOfContents();
    window.addEventListener('scroll', this.handleScroll); // 添加滚动事件
    this.applyCustomStyles(); // 应用自定义样式
    window.addEventListener('scroll', this.updateReadingProgress);
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll); // 清理事件监听器
    window.removeEventListener('scroll', this.updateReadingProgress);
  },
  methods: {
    async fetchArticleDetails() {
      const articleId = this.$route.params.id; // 从路由参数获取文章ID
      try {
        const response = await axios.post('/api/system/doc/article/details', {
          articleId: articleId, // 请求参数，采用JSON格式
          language: this.articleLanguage // 请求参数，采用JSON格式
        });
        if (response.data.code === 200) {
          this.article = response.data.data; // 更新文章对象
          this.generateTableOfContents(); // 根据文章内容生成目录
        } else {
          console.error('获取文章失败:', response.data.message);
        }
      } catch (error) {
        console.error('请求错误:', error);
      }
    },
    generateTableOfContents() {
      const htmlString = this.article.content;
      const parser = new DOMParser();
      const doc = parser.parseFromString(htmlString, 'text/html');
      
      const headers = [...doc.querySelectorAll('h1, h2, h3')];
      
    },
    handleScroll() {
      const windowHeight = window.innerHeight;
      const documentHeight = document.documentElement.scrollHeight;
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;

      if (scrollTop + windowHeight >= documentHeight) { // 提前100像素出现在底部
        this.showToTopButton = true; // 滑到底部显示按钮
      } else {
        this.showToTopButton = false; // 否则隐藏按钮
      }
    },
 
    
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: 'smooth' // 平滑滚动
      });
      this.showToTopButton = false; // 点击按钮后隐藏按钮
    },
    switchLanguageArticle() {
      if(this.articleLanguage === 'zh'){
        this.articleLanguage = 'en';
        this.fetchArticleDetails(); // 切换语言后重新获取文章详情
      }
      else{
        this.articleLanguage = 'zh';
        this.fetchArticleDetails(); // 切换语言后重新获取文章详情
      }
    },
    shareToWeixin() {
      // 实现微信分享逻辑
    },

    shareToWeibo() {
      // 实现微博分享逻辑
    },

    shareToTwitter() {
      // 实现Twitter分享逻辑
    },

    scrollToSection(id) {
      const element = document.getElementById(id);
      if (element) {
        element.scrollIntoView({ behavior: 'smooth' });
      }
    },

    updateReadingProgress() {
      const windowHeight = window.innerHeight;
      const documentHeight = document.documentElement.scrollHeight - windowHeight;
      const scrolled = window.scrollY;
      
      this.readingProgress = (scrolled / documentHeight) * 100;
    },

    handleLike() {
      this.isLiked = !this.isLiked;
      this.likeCount += this.isLiked ? 1 : -1;
      // 这里可以添加与后端交互的逻辑
    },
  },

  computed: {
    isScrolled() {
      return window.scrollY > 100;
    }
  }
};
</script>


